
package com.kunbo.cn.service.impl.core.sale;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.kunbo.cn.dao.core.sale.SaleWaybillQuantityAdjustDao;
import com.kunbo.cn.entity.core.sale.SaleWaybillQuantityAdjust;
import com.kunbo.cn.service.core.sale.SaleWaybillQuantityAdjustService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.retu.Pages;

/**
 *
 * @author bjl  
 * @date 2019年4月21日 
 */
@Service
public class SaleWaybillQuantityAdjustServiceImpl implements SaleWaybillQuantityAdjustService{
	@Autowired
	private SaleWaybillQuantityAdjustDao dao;
	@Autowired
    private JdbcTemplate jdbcTemplate;
	
	@Override
	public void save(SaleWaybillQuantityAdjust swqa) {
		swqa.setCreate_time(SDate.getSystemDateToString());
		dao.save(swqa);
	}

	@Override
	public List<Map<String, Object>> list(String sale_waybill_code) {
		String sql = "SELECT * FROM business_sale_waybill_quantity_adjust s where 1=1 and s.sale_waybill_code='"+sale_waybill_code+"'  order by s.create_time desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	@Override
	public List<Map<String, Object>> WaybillList() {
		String sql = "SELECT w.waybill_code,w.capacity_to_be_transported,w.customer,w.material,w.trans_total,w.supplier,w.transit_compy,w.order_margin,w.executed_amount FROM business_waybill_manage w  WHERE 1 = 1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);//and p.state <> 5 and p.state <> 6
		return list;
	}
	@Override
	public Pages WaybillList1(Integer page,Integer pageSize) {
		String sql1 = "SELECT w.waybill_code,w.capacity_to_be_transported,w.customer,w.material,w.trans_total,w.supplier,w.transit_compy,w.order_margin,w.executed_amount FROM business_waybill_manage w  WHERE 1 = 1";
		String sql2="";
		sql2 += " order by w.id  desc  limit " + (page * pageSize) +"," + pageSize;
		  String sql=sql1.concat(sql2);//获取数据sql
		  List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		  Pages pages =new Pages();
		  Integer count = jdbcTemplate.queryForList(sql1).size();
		  pages.setTotal(count);
		  pages.setData(list);
		return pages;
	}
	/**
	 *
	 * 
	 */
	@Override
	public int AdjustWaybillNum(String waybill_code,String trans_total,String capacity_to_be_transported,String adjust_amount) throws SQLException {
		 int i=0;;
		  //if((sale_order_code!=null && !adjust_amount.isEmpty())&&(newOrder!=null && !newOrder.isEmpty())) {
		Float trans_totals =Float.valueOf(trans_total);
		Float capacity_to_be_transporteds =Float.valueOf(capacity_to_be_transported);
		Float adjust_amounts =Float.valueOf(adjust_amount);
		Float new_trans_total=trans_totals+adjust_amounts;
		Float new_capacity_to_be_transported=capacity_to_be_transporteds+adjust_amounts;
		if((waybill_code!=null && !adjust_amount.isEmpty())){
		//1.修改调量订单(先查询余量)
			String sql = "UPDATE \r\n" +
	        "business_waybill_manage d \r\n"+
	        " set d.trans_total="+new_trans_total+"\r\n "+
	        "  ,d.capacity_to_be_transported="+new_capacity_to_be_transported+"\r\n "+
	        " WHERE  d.waybill_code='"+waybill_code+"'";
			 i=jdbcTemplate.update(sql);
			 System.out.println("【【【【sql【]]]]]]"+sql);
	}
		return i;
	}
	@Override
	public int AdjustExecutedNum(String waybill_code,String executed_amount,String capacity_to_be_transported,String adjust_amount) throws SQLException {
		 int i=0;;
		  //if((sale_order_code!=null && !adjust_amount.isEmpty())&&(newOrder!=null && !newOrder.isEmpty())) {
		 //Float trans_totals =Float.valueOf(trans_total);
		Float executed_amounts =Float.valueOf(executed_amount);
		Float capacity_to_be_transporteds =Float.valueOf(capacity_to_be_transported);
		Float adjust_amounts =Float.valueOf(adjust_amount);
		Float new_capacity_to_be_transported=capacity_to_be_transporteds+adjust_amounts;
		Float new_executed_amount=executed_amounts-adjust_amounts;
		if((waybill_code!=null && !adjust_amount.isEmpty())){
		//1.修改调量订单(先查询余量)
			String sql = "UPDATE \r\n" +
	        "business_waybill_manage d \r\n"+
	        " set d.executed_amount="+new_executed_amount+"\r\n "+
	        "  ,d.capacity_to_be_transported="+new_capacity_to_be_transported+"\r\n "+
	        " WHERE  d.waybill_code='"+waybill_code+"'";
			 i=jdbcTemplate.update(sql);
			 System.out.println("【【【【sql【]]]]]]"+sql);
	}
		return i;
	}
}
